package Satellites;
import java.util.Calendar;
import java.util.Date;


public class Orbit {
 
 
	public static final double raioTerra = 6378.14; //Raio da Terra em metros
	public static final double G = 398600.8; //Aceleracao Gravitica da Terra em m^3/s^2
	public static final double c = Math.pow(3*10,8); //Velocidade da Luz em m/s^2
	public static final double XKE = 0.074366916; 
	private final static double ERROR = 1E-12; //Erro aceitavel para a anomalia real
	
	
	public final static int nPoints = 200; // Numero de pontos necessarios para desenhar a orbita

	private String name;
    private String line1;
    private String line2;
    private GroundStationPosition base;
    private PassPredictor passPredictor;
    private LEOSatellite leos;
    private TLE tle;
    private Date date;

 
    public Orbit(String name, String line1, String line2) throws IllegalArgumentException, InvalidTleException, SatNotFoundException{
    	this.name = name;
        this.line1 = line1;
    	this.line2 = line2;
        
    	String[] sat_TLE = {name, line1, line2};
    	tle = new TLE(sat_TLE);
    	
    	//Definicao das coordenadas da antena
    	base = new GroundStationPosition(38.74807777, -9.15351972, 91);
    	passPredictor = new PassPredictor(tle, base);
    	leos = new LEOSatellite(tle);
    }
    
 // calculo da previs�o das passagens
    public void CalculateSatelliteParameters(){
    	date = new Date();
    	leos.calculateSatelliteVectors(date);
    	 leos.calculateSatelliteGroundTrack();
         leos.calculateSatPosForGroundStation(base);
    }
   
    //apresenta��o dos valores da previsao
    
    //TODO falta ver de q forma os valores vao ser passados para o resto da APP e para o arduino
    public SatPassTime positionPrediction() throws InvalidTleException, SatNotFoundException{
    	date = new Date();
    	leos.calculateSatelliteVectors(date);
    	 leos.calculateSatelliteGroundTrack();
         leos.calculateSatPosForGroundStation(base);
         return passPredictor.nextSatPass(date, true);

        //System.out.println(leos.getPosition(base, date));
        
    }
    
    public int timeUntilPassing() throws InvalidTleException, SatNotFoundException{
    	Date now = new Date();
    	long epoch = now.getTime();
    	long epoch2 = passPredictor.nextSatPass(now).getStartTime().getTime();
    	long time = (epoch2 - epoch)/1000; 
    	//System.out.println(time);
    	return (int) time;
    }
    
    public double getElevation(){
    	date = new Date();
    	leos.calculateSatelliteVectors(date);
    	leos.calculateSatelliteGroundTrack();
        leos.calculateSatPosForGroundStation(base);
        return leos.getPosition(base, date).getElevation();
    	
    }
   
    public double getAzimtuh(){
    	date = new Date();
    	leos.calculateSatelliteVectors(date);
    	leos.calculateSatelliteGroundTrack();
        leos.calculateSatPosForGroundStation(base);
    	return leos.getPosition(base, date).getAzimuth();
    }
    
    public boolean isSatelliteVisible(){
    	date = new Date();
    	leos.calculateSatelliteVectors(date);
    	leos.calculateSatelliteGroundTrack();
    	leos.calculateSatPosForGroundStation(base);
    	return leos.calculateSatelliteGroundTrack().isAboveHorizon();
    	
    }
    
    public double getFirstAzimuth(){
    	return passPredictor.getFirstAzimuth();
    }
    
    //getters dos valores a mostrar no painel lateral
    public int getSatNumber(){
    	return tle.getCatnum();
    }
    public String getClassification(){
    	return tle.getClassification();
    }
    public int getLaunchYear(){
    	return tle.getYear();
    }
    public double getInclination(){
    	return tle.getIncl();
    }
    public double getOmega(){
    	return tle.getOmegao();
    }
    public double getEccentricity(){
    	return tle.getEccn();
    }
    public double getArgPerigee(){
    	return tle.getArgper();
    }
    public double getMeanAnomaly(){
    	return tle.getMeanan();
    	
    }
    public double getRevs(){
    	return tle.getMeanmo();
    }
    
}